package com.div.tools.viewpager

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import com.div.tools.R
import kotlinx.android.synthetic.main.layout_viewpager.view.*

@SuppressLint("StaticFieldLeak")
object ViewHelper : ViewPager.OnPageChangeListener {
    var tvFrontRow: TextView? = null
    var tvBackRow: TextView? = null
    var viewFrontLine: View? = null
    var viewBackLine: View? = null
    var vp: ViewPagerSlide? = null
    lateinit var mContext: Context

    fun initView(view: View, context: Context) {
        mContext = context
        tvFrontRow = view.tv_front_row
        tvBackRow = view.tv_back
        viewFrontLine = view.view_front_row
        viewBackLine = view.view_back
        vp = view.vp_content
        val tvFront = TextView(context)
        val tvBack = TextView(context)
        tvFront.text = "前排"
        tvBack.text = "后排"
        tvFront.gravity = Gravity.CENTER
        tvBack.gravity = Gravity.CENTER
        tvFrontRow?.setTextColor(ContextCompat.getColor(context, R.color.white))
        tvBackRow?.setTextColor(ContextCompat.getColor(context, R.color.gray))
        viewFrontLine?.visibility = View.VISIBLE
        viewBackLine?.visibility = View.GONE
        val list = ArrayList<View>()
        list.add(tvFront)
        list.add(tvBack)
        view.vp_content.setScanScroll(true)
        view.vp_content.adapter = ViewPagerAdapter(list)
        view.vp_content.addOnPageChangeListener(this)
        tvFrontRow?.setOnClickListener { view.vp_content.setCurrentItem(0, true) }
        tvBackRow?.setOnClickListener {view.vp_content.setCurrentItem(1, true)}
    }

    override fun onPageScrollStateChanged(state: Int) {
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        Log.d("ViewHelper", "position : " + position)
        switchPager(position)
    }

    private fun switchPager(position: Int) {
        if (position == 1) {
            viewBackLine?.visibility = View.VISIBLE
            viewFrontLine?.visibility = View.GONE
            tvBackRow?.setTextColor(ContextCompat.getColor(mContext, R.color.white))
            tvFrontRow?.setTextColor(ContextCompat.getColor(mContext, R.color.gray))
        }else {
            viewBackLine?.visibility = View.GONE
            viewFrontLine?.visibility = View.VISIBLE
            tvBackRow?.setTextColor(ContextCompat.getColor(mContext, R.color.gray))
            tvFrontRow?.setTextColor(ContextCompat.getColor(mContext, R.color.white))
        }
    }

    fun setCurrentItem(item: Int) {
        vp?.setCurrentItem(item)
    }
}